#!/bin/bash

# Before edit !!!!!!
# You can change the Software's version before run this script,
#   but you shouldn't change the version of the dependencies,
#   unless you know what you are doing.

# Configuration
ROOT_PATH=/root
SOURCE_PATH=/usr/local/src
TARGET_PATH=/opt/
CORE=16

# Softwares
HTTPD=httpd-2.4.62

#################################################
# Functions
#################################################
# 1: directory name
# 2: download url
function Download() {
    local archive=$1.tar.gz
    if [ ! -e $1 ]; then
        if [ ! -e $archive ]; then
            wget $2
        fi
        tar -zxvf $archive
    fi
}

function Make() {
    make clean
    make -j$CORE
    make install
}

#################################################
# httpd
#################################################
# Dependencies
ZLIB=zlib-1.3.1
LIBICONV=libiconv-1.17
PCRE=pcre-8.39
OPENSSL=openssl-1.1.1w
EXPAT=expat-2.4.9
APR=apr-1.7.4
APR_UTIL=apr-util-1.6.3

function BuildHttpd() {
    # Installation zlib
    cd $SOURCE_PATH
    Download $ZLIB https://zlib.net/$ZLIB.tar.gz
    cd $ZLIB
    ./configure --prefix=$TARGET_PATH
    Make

    # Installation libiconv
    cd $SOURCE_PATH
    Download $LIBICONV https://gitee.com/Ycxtlll/libsources/releases/download/libiconv-1.17/libiconv-1.17.tar.gz
    cd $LIBICONV
    ./configure --prefix=$TARGET_PATH
    Make

    # Installation openssl
    cd $SOURCE_PATH
    Download $OPENSSL https://gitee.com/Ycxtlll/libsources/releases/download/openssl-1.1.1w/openssl-1.1.1w.tar.gz
    cd $OPENSSL
    ./config --prefix=$TARGET_PATH --openssldir=$TARGET_PATH/etc/openssl \
        --with-zlib-include=$TARGET_PATH/include --with-zlib-lib=$TARGET_PATH/lib
    Make
    # remove useless files
    rm -rf $TARGET_PATH/share/man $TARGET_PATH/share/doc

    # Installation pcre
    cd $SOURCE_PATH
    Download $PCRE https://gitee.com/Ycxtlll/libsources/releases/download/pcre-8.39/pcre-8.39.tar.gz
    cd $PCRE
    yum install gcc-c++ -y
    ./configure --prefix=$TARGET_PATH --enable-pcre16 --enable-pcre32 --enable-utf --enable-jit
    Make

    # Installation expat
    cd $SOURCE_PATH
    Download $EXPAT https://gitee.com/Ycxtlll/libsources/releases/download/expat-2.4.9/$EXPAT.tar.gz
    cd $EXPAT
    ./configure --prefix=$TARGET_PATH --enable-shared --enable-static --enable-xml-test --enable-xml-attrdump \
        --enable-xml-xpath --enable-xml-xpath-test --enable-xml-xpath-dump --enable-xml-xpath-dump-test \
        --enable-xml-xpath-dump-test-2 --enable-xml-xpath-dump-test-3 --enable-xml-xpath-dump-test-4 --enable-xml-xpath-d
    Make

    # Installation apr
    cd $SOURCE_PATH
    Download $APR https://archive.apache.org/dist/apr/$APR.tar.gz
    cd $APR
    sed -i '35085s/$RM "$cfgfile"/$RM -r "$cfgfile"/' configure
    ./configure --prefix=$TARGET_PATH
    Make

    # Installation apr-util
    cd $SOURCE_PATH
    Download $APR_UTIL https://archive.apache.org/dist/apr/$APR_UTIL.tar.gz
    cd $APR_UTIL
    yum install -y openldap-devel
    ./configure --prefix=$TARGET_PATH --with-apr=$TARGET_PATH --with-expat=$TARGET_PATH \
        --with-openssl=$TARGET_PATH --with-iconv=$TARGET_PATH --with-ldap
    Make

    # Installation httpd
    cd $SOURCE_PATH
    Download $HTTPD https://archive.apache.org/dist/httpd/$HTTPD.tar.gz
    cd httpd-2.4.62
    ./configure --prefix=$TARGET_PATH \
        --enable-nonportable-atomics \
        --enable-auth-anon \
        --enable-auth-dbm \
        --enable-auth-digest \
        --enable-file-cache \
        --enable-echo \
        --enable-charset-lite \
        --enable-cache \
        --enable-disk-cache \
        --enable-mem-cache \
        --enable-ext-filter \
        --enable-case-filter \
        --enable-case-filter-in \
        --enable-deflate \
        --enable-mime-magic \
        --enable-cern-meta \
        --enable-expires \
        --enable-headers \
        --enable-usertrack \
        --enable-unique-id \
        --enable-proxy \
        --enable-proxy-connect \
        --enable-proxy-ftp \
        --enable-proxy-http \
        --enable-bucketeer \
        --enable-http \
        --enable-info \
        --enable-suexec \
        --enable-cgid \
        --enable-vhost-alias \
        --enable-speling \
        --enable-rewrite \
        --enable-so \
        --enable-dav \
        --enable-dav-fs \
        --enable-mods-shared=most \
        --enable-ldap \
        --enable-auth-ldap \
        --enable-authnz-ldap \
        --enable-ipv6 \
        --enable-dbd \
        --enable-https \
        --enable-modules=all \
        --enable-ssl \
        --sysconfdir=$TARGET_PATH/etc \
        --with-mpm=prefork \
        --with-expat=$TARGET_PATH \
        --with-apr=$TARGET_PATH \
        --with-apr-util=$TARGET_PATH \
        --with-ssl=$TARGET_PATH \
        --with-pcre=$TARGET_PATH/bin/pcre-config \
        --with-z=$TARGET_PATH
    Make
}

BuildHttpd
# Archive httpd and sha256
cd $TARGET_PATH
cp -r lib64/* lib/
rm -r lib64
tar zcvf bit-patch-$HTTPD.tar.gz bin build build-1 cgi-bin include lib man manual modules share
sha256sum bit-patch-$HTTPD.tar.gz >bit-patch-$HTTPD.sha256
mv bit-patch-$HTTPD.tar.gz ~
mv bit-patch-$HTTPD.sha256 ~
